<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SimpleVehicleSFunction</title>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
  tex2jax: { inlineMath: [['$','$'], ['\\(','\\)']] },
  TeX: { equationNumbers: { autoNumber: 'all' } }
});
</script>
<script type="text/javascript" async src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_CHTML"></script>
<style>
body > * {
  max-width: 1000px;
}
body {
  font-family: "Roboto Condensed", sans-serif;
  padding-left: 7.5em;
  padding-right: 7.5em;
}
pre, code {
  max-width: 60em;
  font-family: monospace;
}
pre.oct-code {
  border: 1px solid Grey;
  padding: 5px;
}
pre.oct-code-output {
  margin-left: 2em;
}
span.comment {
  color: ForestGreen;
}
span.keyword {
  color: Blue;
}
span.string {
  color: DarkOrchid;
}
footer {
  margin-top: 2em;
  font-size: 80%;
}
a, a:visited {
  color: Blue;
}
h2 {
  font-family: "Roboto Condensed", serif;
  margin-top: 1.5em;
}
h2 a, h2 a:visited {
  color: Black;
}
</style>
</head>
<body>
<h2>Contents</h2><ul><li><a href="#node1">See Also</a></li></ul><pre class="oct-code"><span class="keyword">function</span> [sys,x0,str,ts] = SimpleVehicleSFunction(t,x,u,flag)
<span class="comment">% This file is a s-function template for simulating the simple vehicle model in Simulink.</span>

<span class="comment">% Choosing tire model</span>
TireModel = TirePacejka();
<span class="comment">% Defining tire parameters</span>
TireModel.a0 = 1;
TireModel.a1 = 0;
TireModel.a2 = 800;
TireModel.a3 = 3000;
TireModel.a4 = 50;
TireModel.a5 = 0;
TireModel.a6 = 0;
TireModel.a7 = -1;
TireModel.a8 = 0;
TireModel.a9 = 0;
TireModel.a10 = 0;
TireModel.a11 = 0;
TireModel.a12 = 0;
TireModel.a13 = 0;

<span class="comment">% Choosing vehicle model</span>
VehicleModel = VehicleSimpleNonlinear();
<span class="comment">% Defining vehicle parameters</span>
VehicleModel.mF0 = 700;
VehicleModel.mR0 = 600;
VehicleModel.IT = 10000;
VehicleModel.lT = 3.5;
VehicleModel.nF = 2;
VehicleModel.nR = 2;
VehicleModel.wT = 2;
VehicleModel.muy = .8;
VehicleModel.tire = TireModel;

<span class="keyword">switch</span> flag,

  <span class="comment">%%%%%%%%%%%%%%%%%%</span>
  <span class="comment">% Initialization %</span>
  <span class="comment">%%%%%%%%%%%%%%%%%%</span>
  <span class="keyword">case</span> 0,
    [sys,x0,str,ts]=mdlInitializeSizes();

  <span class="comment">%%%%%%%%%%%%%%%</span>
  <span class="comment">% Derivatives %</span>
  <span class="comment">%%%%%%%%%%%%%%%</span>
  <span class="keyword">case</span> 1,
    sys=mdlDerivatives(t,x,u,VehicleModel);

  <span class="comment">%%%%%%%%%%%</span>
  <span class="comment">% Outputs %</span>
  <span class="comment">%%%%%%%%%%%</span>
  <span class="keyword">case</span> 3,
    sys=mdlOutputs(t,x,u,VehicleModel);

  <span class="comment">%%%%%%%%%%%%%%%%%%%</span>
  <span class="comment">% Unhandled flags %</span>
  <span class="comment">%%%%%%%%%%%%%%%%%%%</span>
  <span class="keyword">case</span> { 2, 4, 9 },
    sys = [];

  <span class="comment">%%%%%%%%%%%%%%%%%%%%</span>
  <span class="comment">% Unexpected flags %</span>
  <span class="comment">%%%%%%%%%%%%%%%%%%%%</span>
  <span class="keyword">otherwise</span>
    DAStudio.error(<span class="string">'Simulink:blocks:unhandledFlag'</span>, num2str(flag));

<span class="keyword">end</span>
<span class="comment">% end csfunc</span>

<span class="comment">%</span>
<span class="comment">%=============================================================================</span>
<span class="comment">% mdlInitializeSizes</span>
<span class="comment">% Return the sizes, initial conditions, and sample times for the S-function.</span>
<span class="comment">%=============================================================================</span>
<span class="comment">%</span>
<span class="keyword">function</span> [sys,x0,str,ts]=mdlInitializeSizes()

<span class="comment">% Definitions</span>
sizes = simsizes;
sizes.NumContStates  = 6;
sizes.NumDiscStates  = 0;
sizes.NumOutputs     = 6;
sizes.NumInputs      = 3;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 1;

sys = simsizes(sizes);

<span class="comment">% Setting initial conditions</span>
x0  = [0 0 0 20 0 0];
str = [];
ts  = [0 0];

<span class="comment">% end mdlInitializeSizes</span>
<span class="comment">%</span>
<span class="comment">%=============================================================================</span>
<span class="comment">% mdlDerivatives</span>
<span class="comment">% Return the derivatives for the continuous states.</span>
<span class="comment">%=============================================================================</span>
<span class="comment">%</span>


<span class="keyword">function</span> sys = mdlDerivatives(t,x,u,vehicle)

<span class="comment">% Defining input</span>
vehicle.deltaf = u(1);
vehicle.Fxf = u(2);
vehicle.Fxr = u(3);

<span class="comment">% Getting the vehicle model function (state equations)</span>
ModelFunction = @vehicle.Model;

sys = ModelFunction(t,x,0);

<span class="comment">% end mdlDerivatives</span>
<span class="comment">%</span>
<span class="comment">%=============================================================================</span>
<span class="comment">% mdlOutputs</span>
<span class="comment">% Return the block outputs.</span>
<span class="comment">%=============================================================================</span>
<span class="comment">%</span>
<span class="keyword">function</span> sys=mdlOutputs(~,x,~,~)

<span class="comment">% Output are all state variables</span>
sys = x;

<span class="comment">% end mdlOutputs</span>
</pre><h2><a id="node1">See Also</a></h2><p>[<a href="../index.html">Home</a>] [<a href="TemplateSimpleSimulink.html">Template Simple Simulink</a>]</p>
<footer><hr><a href="http://www.octave.org">Published with GNU Octave 4.2.1</a></footer>
<!--
##### SOURCE BEGIN #####
function [sys,x0,str,ts] = SimpleVehicleSFunction(t,x,u,flag)
% This file is a s-function template for simulating the simple vehicle model in Simulink.

% Choosing tire model
TireModel = TirePacejka();
% Defining tire parameters
TireModel.a0 = 1;
TireModel.a1 = 0;
TireModel.a2 = 800;
TireModel.a3 = 3000;
TireModel.a4 = 50;
TireModel.a5 = 0;
TireModel.a6 = 0;
TireModel.a7 = -1;
TireModel.a8 = 0;
TireModel.a9 = 0;
TireModel.a10 = 0;
TireModel.a11 = 0;
TireModel.a12 = 0;
TireModel.a13 = 0;

% Choosing vehicle model
VehicleModel = VehicleSimpleNonlinear();
% Defining vehicle parameters
VehicleModel.mF0 = 700;
VehicleModel.mR0 = 600;
VehicleModel.IT = 10000;
VehicleModel.lT = 3.5;
VehicleModel.nF = 2;
VehicleModel.nR = 2;
VehicleModel.wT = 2;
VehicleModel.muy = .8;
VehicleModel.tire = TireModel;

switch flag,

  %%%%%%%%%%%%%%%%%%
  % Initialization %
  %%%%%%%%%%%%%%%%%%
  case 0,
    [sys,x0,str,ts]=mdlInitializeSizes();

  %%%%%%%%%%%%%%%
  % Derivatives %
  %%%%%%%%%%%%%%%
  case 1,
    sys=mdlDerivatives(t,x,u,VehicleModel);

  %%%%%%%%%%%
  % Outputs %
  %%%%%%%%%%%
  case 3,
    sys=mdlOutputs(t,x,u,VehicleModel);

  %%%%%%%%%%%%%%%%%%%
  % Unhandled flags %
  %%%%%%%%%%%%%%%%%%%
  case { 2, 4, 9 },
    sys = [];

  %%%%%%%%%%%%%%%%%%%%
  % Unexpected flags %
  %%%%%%%%%%%%%%%%%%%%
  otherwise
    DAStudio.error('Simulink:blocks:unhandledFlag', num2str(flag));

end
% end csfunc

%
%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
%
function [sys,x0,str,ts]=mdlInitializeSizes()

% Definitions
sizes = simsizes;
sizes.NumContStates  = 6;
sizes.NumDiscStates  = 0;
sizes.NumOutputs     = 6;
sizes.NumInputs      = 3;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 1;

sys = simsizes(sizes);

% Setting initial conditions
x0  = [0 0 0 20 0 0];
str = [];
ts  = [0 0];

% end mdlInitializeSizes
%
%=============================================================================
% mdlDerivatives
% Return the derivatives for the continuous states.
%=============================================================================
%


function sys = mdlDerivatives(t,x,u,vehicle)

% Defining input
vehicle.deltaf = u(1);
vehicle.Fxf = u(2);
vehicle.Fxr = u(3);

% Getting the vehicle model function (state equations)
ModelFunction = @vehicle.Model;

sys = ModelFunction(t,x,0);

% end mdlDerivatives
%
%=============================================================================
% mdlOutputs
% Return the block outputs.
%=============================================================================
%
function sys=mdlOutputs(~,x,~,~)

% Output are all state variables
sys = x;

% end mdlOutputs

%% See Also
%
% [<../index.html Home>] [<TemplateSimpleSimulink.html Template Simple Simulink>]
%
##### SOURCE END #####
-->
</body>
</html>
